<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" placeholder="用户编号(交易方)" />
      <el-input v-model="listQuery.incomeUserId" clearable class="filter-item" style="width: 200px;" placeholder="用户编号(收益方)" />
      <el-select v-model="listQuery.disType" class="filter-item" placeholder="分销类型" title="分销类型"><el-option v-for="(item, index) in disTypeDic" :key="index" :label="item" :value="index" /></el-select>
      <el-select v-model="listQuery.disLevel" class="filter-item" placeholder="层级" title="层级"><el-option v-for="(item, index) in disLevelDic" :key="index" :label="item" :value="index" /></el-select>
      <el-select v-model="listQuery.status" class="filter-item" placeholder="状态" title="状态"><el-option v-for="(item, index) in statusDic" :key="index" :label="item" :value="index" /></el-select>
      <el-input v-model="listQuery.orderSn" clearable class="filter-item" style="width: 200px;" placeholder="商品订单" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="序号" prop="id" sortable />
      <el-table-column align="center" label="交易用户" prop="userId" sortable />
      <el-table-column align="center" label="收益用户" prop="incomeUserId" />
      <el-table-column align="center" label="收益金额" prop="actual_price" />
      <el-table-column align="center" label="分销类型" prop="dis_type" sortable>
        <template slot-scope="scope">
          <el-tag>{{ disTypeDic[scope.row.dis_type] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="层级" prop="dis_level" sortable>
        <template slot-scope="scope">
          <el-tag>{{ disLevelDic[scope.row.dis_level] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="费率" prop="dis_rate">
        <template slot-scope="scope">
          {{ scope.row.goodsVoList[0].number | percentage }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="商品订单" prop="order_sn" />
      <el-table-column align="center" label="描述" prop="goodsVoList">
        <template slot-scope="scope">
          {{ scope.row.goodsVoList[0].goodsName }} ￥{{ scope.row.goodsVoList[0].price }}  {{ scope.row.goodsVoList[0].number }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status" sortable>
        <template slot-scope="scope">
          <el-tag>{{ disLevelDic[scope.row.income_status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入账时间" prop="update_time" />
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.income_status == 0" type="primary" size="mini" @click="handleDetail(scope.row)">入账</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="分润入账">
      <el-table v-loading="listLoading" :data="orderNos" element-loading-text="正在查询中。。。" border fit highlight-current-row>
        <el-table-column align="center" label="订单号" prop="order_sn" />
        <el-table-column align="center" label="分销编号" prop="id" sortable />
        <el-table-column align="center" label="收益用户" prop="incomeUserId" />
        <el-table-column align="center" label="层级" prop="dis_level" sortable>
          <template slot-scope="scope">
            <el-tag>{{ disLevelDic[scope.row.dis_level] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="分销费率" prop="dis_rate" />
        <el-table-column align="center" label="收益金额" prop="actual_price" />
        <el-table-column align="center" label="状态" prop="status" sortable>
          <template slot-scope="scope">
            <el-tag>{{ disLevelDic[scope.row.income_status] }}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doUpdate">确定已付款</el-button>
        <el-button @click="userDialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { disOrderList, updateDisOrder } from '@/api/withdraw'

export default {
  name: 'DistributionOrderList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        incomeUserId: undefined,
        disType: undefined,
        disLevel: undefined,
        status: undefined,
        orderSn: undefined,
        sort: 'update_time',
        order: 'ASC'
      },
      downloadLoading: false,
      disTypeDic: ['体验商品', '会员分销'],
      disLevelDic: ['直推', '接推'],
      statusDic: ['未入账', '已入账'],
      userDialogVisible: false,
      orderNos: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList: function() {
      this.listLoading = true
      disOrderList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    doUpdate() {
      debugger
      if (!this.orderNos) {
        return false
      }
      var order_sn = null
      if (this.orderNos && this.orderNos instanceof Array && this.orderNos.length > 0) {
        order_sn = this.orderNos[0].order_sn
      } else {
        order_sn = this.orderNos.order_sn
      }
      updateDisOrder({ 'order_sn': order_sn })
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '提现参数配置成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDetail(row) {
      this.orderNos = this.list.filter(item => item.order_sn == row.order_sn)
      this.userDialogVisible = true
    }
  }
}
</script>
